﻿Imports System.Text.RegularExpressions
Imports Microsoft.Win32
'Imports System.DirectoryServices
Imports System.DirectoryServices.ActiveDirectory
Public Class ADServices
    Public Shared Function BuildFilter()
        Dim strQueryString As String = ""
        'MsgBox(Form1.lstVisualFilter.Items.Count)
        If Form1.lstVisualFilter.Items.Count = 1 Then
            strQueryString = "strfiltername.contains(" & Chr(34) & Form1.lstVisualFilter.Items.Item(0).ToString & Chr(34) & ")"
        Else
            For Each item In Form1.lstVisualFilter.Items
                strQueryString = strQueryString & " Or strfiltername.contains(" & Chr(34) & item.ToString & Chr(34) & ")"
            Next
            strQueryString = strQueryString.Substring(4)
        End If

        Return strQueryString
    End Function
    Public Shared Function BuildRegEx(ByVal strSearch As String)
        Dim myBool As Boolean
        Dim arr1()
        ReDim arr1(Form1.lstVisualFilter.Items.Count - 1)
        Form1.lstVisualFilter.Items.CopyTo(arr1, 0)

        Dim strArrList As String = String.Join("|", arr1)
        'MsgBox(strArrList)
        For Each match As Match In Regex.Matches(strSearch, strArrList)
            Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)

            If match.Success = True Then
                myBool = True
            Else
                myBool = False

            End If
        Next
        Return myBool
    End Function
    Public Shared Function GetActiveBias()
        ' Open the TimeZone key
        Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet" & "\Control\TimeZoneInformation")
        If key Is Nothing Then
            Return 0
        End If

        ' Pick up the time bias
        Dim bias As Integer = CInt(key.GetValue("ActiveTimeBias"))

        ' Close the parent key
        key.Close()

        ' return the result adjusted for hours (instead of minutes)
        Return (Bias)
    End Function

    Public Shared Function ConvertTo64Bit(ByVal dtmDateValue As DateTime)
        Dim lngBiasKey = GetActiveBias()
        Dim lngBias As Long
        Dim lngSeconds As Long
        Dim dtmAdjusted As DateTime

        Dim str64Bit As String

        If (UCase(TypeName(lngBiasKey)) = "LONG") Then
            lngBias = lngBiasKey
        ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then
            lngBias = 0
            For k = 0 To UBound(lngBiasKey)
                lngBias = lngBias + (lngBiasKey(k) * 256 ^ k)
            Next
        End If

        ' Convert datetime value to UTC.
        dtmAdjusted = DateAdd("n", lngBias, dtmDateValue)

        ' Find number of seconds since 1/1/1601.
        lngSeconds = DateDiff("s", #1/1/1601#, dtmAdjusted)

        ' Convert the number of seconds to a string
        ' and convert to 100-nanosecond intervals.
        str64Bit = CStr(lngSeconds) & "0000000"
        Return str64Bit

    End Function
    Public Shared Function EnumDomains() As ArrayList
        Dim alDomains As New ArrayList
        Dim myForest As Forest = Forest.GetCurrentForest
        Dim myDomains As DomainCollection = myForest.Domains
        For Each objDomain In myDomains
            alDomains.Add(objDomain.Name)
        Next
        Return alDomains
    End Function
    Public Function EmunOrgUnits() As ArrayList
        Dim alOrgUnits As New ArrayList
        Return alOrgUnits
    End Function
End Class
